package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.ItemModelDTO;
import com.hang.model.dto.response.ItemQueryListVO;
import com.hang.model.vo.ItemInfoDetailModel;
import com.hang.service.IItemService;
import com.hang.service.IItemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@RestController
@Api(tags = "项目管理模块")
@RequestMapping("item")
@Slf4j
public class ItemController {
    @Resource
    private IItemService itemService;

    @ApiOperation(value = "新建项目")
    @PostMapping("create")
    public Result<Object> createItem(@RequestBody @Valid ItemModelDTO itemModelDTO){
        try {
            itemService.createItem(itemModelDTO);
            return Result.ok().message("创建成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "编辑项目")
    @PostMapping("update")
    public Result<Object> updateItem(
            @RequestBody @Valid ItemModelDTO itemModelDTO
    ){
        try {
            itemService.editItem(itemModelDTO);
            return Result.ok().message("编辑成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "删除项目")
    @DeleteMapping("delete")
    public Result<Object> deleteItem(@RequestParam("itemId") Long itemId){
        try {
            itemService.deleteItem(itemId);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    /**
     * 按项目ID精准搜索
     * 按项目名/项目管理者姓名/创建人 模糊查询
     * @param conditionDTO
     * @return
     */
    @ApiOperation(value = "管理员分页条件查询项目")
    @PostMapping("getItemByCondition")
    public Result<ItemQueryListVO> getItemByCondition(@RequestBody ConditionDTO conditionDTO){
        ItemQueryListVO itemQueryListVO = itemService.getItemsByCondition(conditionDTO);
        return Result.ok(itemQueryListVO);
    }

    /**
     * 获取项目详情
     */
    @ApiOperation(value = "获取项目详情")
    @GetMapping("getItemById")
    public Result<ItemInfoDetailModel> getItemDetailByItemId(@RequestParam("itemId") Long itemId){
        ItemInfoDetailModel itemInfoDetailModel = itemService.getItemDetailByItemId(itemId);
        return Result.ok(itemInfoDetailModel);
    }
}